﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Net.Fan.SmallPig.BusinessRules;
using Net.Fan.SmallPig.DataAccess;
using System.Data;

public partial class RegUser : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            PagePermission.CheckIsAdmin(this);
            //this.LBDepart.Attributes.Add("OnClick", "document.getitembyid(\"txtDuty\").value=document.getitembyid(\"txtDuty\").selected.value");
            getDepts();
            //this.LBDepart.DataSource = ds;
            // this.LBDepart.DataBind();
        }

    }

    private void getDepts()
    {

        List<string> ds = UserDA.GetUsedDepts();
        LBDepart.Items.Clear();
        foreach (var item in ds)
        {
            LBDepart.Items.Add(item);
        }
    }
    protected void btReg_Click(object sender, EventArgs e)
    {
        string pwd;
        string errorMsg;
        if (UserBiz.CheckUserNameString(txtUserName.Text.Trim(), out errorMsg) == false)
        {
            this.labMsg.Text = errorMsg;
            txtUserName.Focus();
            return;
        }

        //若没有填写则默认为与用户名相同
        if (txtPass.Text.Trim() != string.Empty)
        {
            if (UserBiz.CheckPasswordString(txtPass.Text.Trim(), out errorMsg) == false)
            {
                this.labMsg.Text = errorMsg;
                return;
            }
            pwd = this.txtPass.Text.Trim();
        }
        else
        {
            pwd = this.txtUserName.Text.Trim();
        }



        //if (txtPass.Text != txtRePass.Text)
        //{
        //    this.labMsg.Text = "错误,两遍输入的口令不一致!";
        //    txtPass.Focus();
        //    return;
        //}

        if (txtTrueName.Text.Trim().Length == 0)
        {
            this.labMsg.Text = "错误,真实姓名不能为空!";
            txtTrueName.Focus();
            return;
        }

        //if (chkTern.Checked == false)
        //{
        //    labMsg.Text = "错误,您未同意本网站协议!";
        //    chkTern.Focus();
        //    return;
        //}

        //if (String.Compare(Session["ValidateString"] as string, txtValidateString.Text, true) != 0)
        //{
        //    labMsg.Text = "错误,请输入正确的验证码!";
        //    txtValidateString.Focus();
        //    return;
        //}

        UserDA.Insert(txtUserName.Text.Trim(), pwd, txtTrueName.Text, lstSex.SelectedValue, TextBoxDepart.Text.Trim(), txtDuty.Text, TBHomeTel.Text, TBMobile.Text, TBEmail.Text, txtRemark.Text, Request.UserHostAddress);
        this.labMsg.Text = "提示:注册成功!";
        //MailBiz.SendRegSucessfully(txtUserName.Text.Trim());
        //Response.Redirect(string.Format("RegPassed.aspx?U={0}", txtUserName.Text));
    }

    [System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]
    public static string GetDynamicContent(string contextKey)
    {
        return default(string);
    }

    //[System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]
    //public static string GetDynamicContent(string contextKey)
    //{
    //     DataSet dt = Net.Fan.SmallPig.DataAccess.DeptDA.GetDepts();
    //     StringBuilder sb =new StringBuilder();
    //    //foreach (DataRow dr in dt.Tables[0].Rows)
    //    //{
    //    //   //<asp:ListItem></asp:ListItem>
    //    //    sb.Append("<asp:ListItem>"+dr["parentName"].ToString()+"</asp:ListItem>");

    //    //}
    //     return sb.ToString();
    //    //return default(string);
    //}

    //[System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]
    //public static string GetDynamicContent2(string contextKey)
    //{
    //    return default(string);
    //}


    protected void OnSelect(object sender, EventArgs e)
    {
        TextBoxDepart.Text = ((LinkButton)sender).Text;
    }


    protected void LBDepart_SelectedIndexChanged(object sender, EventArgs e)
    {
        TextBoxDepart.Text = LBDepart.SelectedItem.Text;
        //getDepts();
    }
}
